package com.wangdao.mall.controller.client;

import com.google.gson.Gson;
import com.wangdao.mall.model.*;
import com.wangdao.mall.service.OrderService;
import com.wangdao.mall.service.OrderServiceImp;
import com.wangdao.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 17:10 2020/5/6
 * @Modified By:
 */
@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {

    private OrderService orderService = new OrderServiceImp();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/order/", "");
        if ("addOrder".equals(action)){
            addOrder(request,response);
        }else if ("settleAccounts".equals(action)){
            settleAccounts(request,response);
        }
    }

    /**
     * 结算购物车
     * @param request
     * @param response
     */
    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        CartLists cartList = gson.fromJson(requestBody, CartLists.class);
        orderService.settleAccounts(cartList);
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 添加到购物车
     * @param request
     * @param response
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Cart cart = gson.fromJson(requestBody, Cart.class);
        int row = orderService.addOrder(cart);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("添加购物车失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/order/", "");
        if ("getOrderByState".equals(action)){
            getOrderByState(request,response);
        }else if ("deleteOrder".equals(action)){
            deleteOrder(request,response);
        }else if ("confirmReceive".equals(action)){
            confirmReceive(request,response);
        }else if ("pay".equals(action)){
            pay(request,response);
        }
    }

    /**
     * 确认付款
     * @param request
     * @param response
     */
    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int row = orderService.pay(id);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("付款失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 确认收货
     * @param request
     * @param response
     */
    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int row = orderService.confirmReceive(id);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("确认失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 删除订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int row = orderService.deleteOrder(id);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("删除失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String state = request.getParameter("state");
        String token = request.getParameter("token");
        List<OrderList> orderList = orderService.getOrderByState(state,token);
        Result result = new Result();
        result.setCode(0);
        result.setData(orderList);
        response.getWriter().println(gson.toJson(result));
    }
}
